package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.User;
import com.ysd.util.DBManager;

public class UserDaoImpl {
	// 增删改时,应该返回数据库受影响的行数
	public int addMessage(User user) {
		return DBManager.executeUpdate("insert into user(id,username,phone,email,content) values(null,'"+user.getUsername()+"','"+user.getPhone()+"','"+user.getEmail()+"','"+user.getContent()+"')");
	}
	/**
	 * 修改留言状态
	 * @param user
	 * @return
	 */
	public int updateMessageById(User user) {
		return DBManager.executeUpdate("update user set status='"+user.getStatus()+"' where id="+user.getId());
	}
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	public int deleteMessageByIds(String ids) {
		String sql = "delete from user where id in("+ids+")";
		System.out.println(sql) ;
		return DBManager.executeUpdate(sql);
	}
	
	public int updateMessageReplyById(User user) {
		String sql ="update user set status='已回复',reply='"+user.getReply()+"' where id="+user.getId();
		System.out.println(sql) ;
		return DBManager.executeUpdate(sql);
	}
	
	public List<User> getUserByStatus(String status){
		List<User> list = new ArrayList<User>();
		Connection conn=DBManager.getConnection();
		Statement st=null;
		ResultSet rs=null;
		try {
			st=conn.createStatement();
			String sql = "select * from user where 1=1" ;
			if(status!=null && !status.equals("")) {
				sql +=" and status='"+status+"'";
			}
			sql +=" order by time desc";
			System.out.println(sql) ;
			rs=st.executeQuery(sql);
			while(rs.next()) {
				int id=rs.getInt("id"); 
				String username=rs.getString("username");
				String phone=rs.getString("phone");
				String email=rs.getString("email");
				String content=rs.getString("content");
				String sta=rs.getString("status");
				String time=rs.getString("time");
				User user = new User(id, username, phone, email, content, sta, time);
				list.add(user);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
		
	}
	public List<User> getUserByStatusMyPage(String status,int page,int size){
		List<User> list = new ArrayList<User>();
		Connection conn=DBManager.getConnection();
		Statement st=null;
		ResultSet rs=null;
		try {
			st=conn.createStatement();
			String sql = "select * from user where 1=1" ;
			if(status!=null && !status.equals("")) {
				sql +=" and status='"+status+"'";
			}
			sql +=" order by time desc";
			sql +=" limit "+(page-1)*size+","+size;
			System.out.println(sql) ;
			rs=st.executeQuery(sql);
			while(rs.next()) {
				int id=rs.getInt("id"); 
				String username=rs.getString("username");
				String phone=rs.getString("phone");
				String email=rs.getString("email");
				String content=rs.getString("content");
				String sta=rs.getString("status");
				String time=rs.getString("time");
				User user = new User(id, username, phone, email, content, sta, time);
				list.add(user);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
		
	}
	
	
	public User getUserById(int id){
		User user =null;
		Connection conn=DBManager.getConnection();
		Statement st=null;
		ResultSet rs=null;
		try {
			st=conn.createStatement();
			String sql = "select * from user where id="+id ;
			
			System. out. println(sql) ;
			rs=st.executeQuery(sql);
			while(rs.next()) {
				String username=rs.getString("username");
				String phone=rs.getString("phone");
				String email=rs.getString("email");
				String content=rs.getString("content");
				String sta=rs.getString("status");
				String time=rs.getString("time");
				user = new User(id, username, phone, email, content, sta, time);
				
			}
			return user;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
		
	}
	/**
	 * 根据留言id删除留言
	 * @param zxId
	 * @return
	 */
	public int deluserId(int id) {
		return DBManager.executeUpdate("DELETE FROM user WHERE id="+id);
	}
	
}
